package com.miui.theme.custom.screen

import android.R.attr.contentDescription
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ExitToApp
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ExitToApp
import androidx.compose.material.icons.outlined.DateRange
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.miui.theme.custom.components.AppTopAppBar
import com.miui.theme.custom.navigation.AppScreen
import com.miui.theme.custom.navigation.currentComposeNavigator
import com.miui.theme.custom.viewmodel.MainViewModel
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalMaterial3Api::class) // 需要为 ListItem
@Composable
fun MainScreen(viewModel: MainViewModel = koinViewModel()) {
    val navigator = currentComposeNavigator
    val tables by viewModel.tables.collectAsState() //

    Scaffold(
        topBar = {
            AppTopAppBar(
                title = "数据库主页",
                actions = {
                    IconButton(onClick = {
                        viewModel.disconnect()
                        navigator.navigateAndClearBackStack(AppScreen.Login)
                    }) {
                        Icon(
                            imageVector = Icons.Default.ExitToApp,
                            contentDescription = "退出登录"
                        )
                    }
                }
            )
        }, //
        floatingActionButton = {
            FloatingActionButton(onClick = {
                navigator.navigate(AppScreen.CreateTable)
            }) {
                Icon(Icons.Default.Add, contentDescription = "创建表")
            }
        }
    ) { padding ->

        if (tables.isEmpty()) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "没有找到数据表。\n点击右下角按钮创建一个。",
                    style = MaterialTheme.typography.bodyLarge,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    textAlign = TextAlign.Center
                )
            }
        } else {
            LazyColumn(
                modifier = Modifier
                    .padding(padding)
                    .fillMaxSize()
            ) {
                items(tables) { tableName ->
                    ListItem(
                        headlineContent = {
                            Text(tableName, style = MaterialTheme.typography.titleMedium)
                        },
                        leadingContent = {
                            Icon(
                                Icons.Outlined.DateRange,
                                contentDescription = "Table Icon"
                            )
                        },
                        modifier = Modifier
                            .clickable {
                                navigator.navigate(AppScreen.TableCrud(tableName)) //
                            }
                    )
                    HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp)) // M3 的 Divider
                }
            }
        }
    }
}